﻿/*
创建人：zhangrj
日期：2022-01-14
描述：批次属性维护、工序、配置表属性维护、工序不加工维护-页面js
*/

//批次列表
var page = {
    pageType: getParam("type"), //type:1工序不加工，2工序属性（备注）、3批次属性、4配置表属性、5工序报工备注维护
    pagetotal: 0,
    pageSize: 20,
    pageNumber: 1,
    url: getRootPath() + "/Ashx/YePianContainer/YPContainerStepAttributeHandler.ashx", //后台处理逻辑url
    init: function () {
        customizedEasyuiPagination('pp'); //定制化分页
        page.bind();
        page.initData();
    },
    //初始化页面
    initData: function () {
        $("#stepNoShip").hide(); //工序不加工隐藏
        $("#containerAttrFillSec").hide(); //批次属性隐藏
        $("#stepAttrFillSec").hide(); //工序备注维护隐藏
        $("#accessoriesFillSec").hide(); //配置表维护隐藏
        $("#stepWorkReportSec").hide(); //报工维护隐藏
        var typearr = page.pageType.split(',');
        if (page.pageVaild(typearr)) {
            for (var i = 0; i < typearr.length; i++) {
                switch (typearr[i]) {
                    case "1":
                        $("#stepNoShip").show(); //显示工序不加工维护
                        stepNoShip.init();
                        break;
                    case "2":
                        $("#stepAttrFillSec").show(); //显示工序属性维护
                        stepAttr.init();
                        break;
                    case "3":
                        $("#containerAttrFillSec").show(); //显示批次属性维护
                        containerAttr.init();
                        break;
                    case "4":
                        $("#accessoriesFillSec").show(); //显示配置表属性维护
                        accessoriesAttr.init();
                        break;
                    case "5":
                        $("#stepWorkReportSec").show(); //显示报工备注维护
                        stepWorkReport.init();
                        break;
                    default:
                        break;
                }
            }
        }
        page.initPageData();
    },
    //初始化页面事件
    bind: function () {
        //批次主列表 update:wangjh 增加分组功能 2022-4-17
        $("#gdContainerTable").datagrid({
            groupField: 'PRODUCTNAME',
            //view: groupview,
            groupFormatter: function (value, rows) {
                var val = value + ' - ' + rows.length + ' Item(s)'
                return val; //"<span data-count=" + rows.length + " class='customTitle' >" + val + "</span>";
            },
            onLoadSuccess: function (data) {
                $('#pp').pagination('refresh', {	// 改变选项，并刷新分页栏信息
                    total: page.pagetotal,
                    pageSize: page.pageSize,
                    pageNumber: page.pageNumber
                });
                customizedEasyuiPagination('pp'); //定制化分页

                //处理分组下只有一个项的，隐藏分组按钮
                //                var dataGroups = $("#dvContainerTable .datagrid-body").find(".datagrid-group");
                //                if (dataGroups.length > 0) {
                //                    $.each(dataGroups, function (index, item) {
                //                        if ($(item).find('.customTitle').data('count') == 1) {
                //                            var groupIndex = $(item).attr('group-index');
                //                            //隐藏
                //                            $("#dvContainerTable").find(".datagrid-group[group-index=" + groupIndex + "]").css("display","none");
                //                        }
                //                    });
                //                }

            },
            columns: [[
                 { field: 'ck', title: '选择', checkbox: true },
                 { field: 'MFGORDERNAME', title: '订单号', width: 120 },
		         { field: 'PRODUCTBATCH', title: '流水卡', width: 180 },
		         { field: 'SUMQTY', title: '在制数', width: 60 },
                 { field: 'CONTAINERTYPENAME', title: '流水卡类型', width: 120 },
                 { field: 'PRODUCTDESCRIPTION', title: '产品名称', width: 180 },
                 { field: 'WORKFLOWNAME', title: '工艺', width: 150 },
                 { field: 'WORKFLOWREVISION', title: '工艺版本', width: 80 },
                 { field: 'WORKFLOWSTEPNAME', title: '当前工序', width: 150 },
                 { field: 'PLANNEDSTARTDATE', title: '计划开始', width: 120,
                     formatter: function (value) {
                         if (value !== null) {
                             return value.replace("T", "").replace("00:00:00", "").replace("0:00:00", "");
                         }
                     }
                 },
                 { field: 'PLANNEDCOMPLETIONDATE', title: '计划完成', width: 120, formatter: function (value) {
                     if (value !== null) {
                         return value.replace("T", "").replace("00:00:00", "").replace("0:00:00", "");
                     }
                 }
                 },
                   { field: 'CONTAINERTYPE', hidden: true },
                 { field: 'TOGETHERFLAG', hidden: true },
                 { field: 'CONTAINERID', hidden: true },
                 { field: 'WORKFLOWID', hidden: true },
                 { field: 'PRODUCTNAME', hidden: true },
                 { field: 'PRODUCTID', hidden: true },
                 { field: 'CONTAINERNO', hidden: true },
                 { field: 'WORKFLOWSTEPSEQUENCE', hidden: true }
                ]],
            onCheck: function (index, rowData) {
                var checkedData = $("#gdContainerTable").datagrid("getChecked");
                page.searchDataDetail(checkedData);
            },
            onUncheck: function (index, rowData) {
                var checkedData = $("#gdContainerTable").datagrid("getChecked");
                page.searchDataDetail(checkedData);
            },
            onCheckAll: function (index, rowData) {
                var checkedData = $("#gdContainerTable").datagrid("getChecked");
                page.searchDataDetail(checkedData);
            },
            onUncheckAll: function (index, rowData) {
                var checkedData = $("#gdContainerTable").datagrid("getChecked");
                page.searchDataDetail(checkedData);
            }
        });
        //批次号列表加载
        $("#gdContainerInfo2").datagrid({
            columns: [[
                 { field: 'ck', title: '选择', checkbox: true },
		         { field: 'CONTAINERNAME', title: '流水卡号', width: 200 },
                 { field: 'QTY', title: '在制数', width: 80 },
                 { field: 'CONTAINERID', title: '批次ID', hidden: true },
                 { field: 'WORKFLOWID', title: '工艺ID', hidden: true },
                 { field: 'MFGORDERNAME', title: '订单ID', hidden: true },
                 { field: 'PRODUCTID', title: '件号ID', hidden: true },
                 { field: 'BOMID', hidden: true }
	           ]],
            onCheck: function (rowIndex, rowData) {
                page.gd2DatagridEvent();
            },
            onUncheck: function (rowIndex, rowData) {
                page.gd2DatagridEvent();
            },
            onCheckAll: function (rows) {
                page.gd2DatagridEvent();
            },
            onUncheckAll: function (rows) {
                page.gd2DatagridEvent();
            }
        });
        //分页事件
        $('#pp').pagination({
            onSelectPage: function (pageNumber, pageSize) {
                customizedEasyuiPagination('pp'); //定制化分页
                page.pageSize = pageSize; page.pageNumber = pageNumber;
                page.search();
            }
        });
        $(document).ready(function () {
            //扫描批次事件
            $('#txtScanContainer').textbox({
                onChange: function (n, o) {
                    page.pageNumber = 1;
                    page.search();
                }
            });
        });
        //查询
        $("#btnSearch").on("click", function () {
            page.pageNumber = 1;
            page.clearPageData(false, true, true, true, true, true, true, true);
            page.search();
        });
        //查询重置
        $("#btnReset").on("click", function () {
            page.clearPageData(true, true, true, true, true, true, true, true);
        });
    },
    //初始化页面数据
    initPageData: function () {
        page.post(page.url, { action: "InitPageData", paramType: page.pageType }, true, function (res) {
            if (res.Result) {
                var dtFactory = res.DBDataSet.dtFactory;
                //绑定工厂下拉框
                $("#selFactory").combobox({
                    valueField: 'FACTORYID',
                    textField: 'FACTORYNAME',
                    panelHeight: 200,
                    data: dtFactory, //下拉数据
                    value: dtFactory[0].FACTORYID,
                    onSelect: function (record) {
                        page.clearPageData(false, true, true, true, true, true, true, true);
                    }
                });
            }
        });
    },
    //查询批次列表
    searchDataDetail: function (rowDatas) {
        page.clearPageData(false, false, true, true, true, true, true, true);
        var factoryId = $('#selFactory').combobox('getValue');
        if (page.isVaildContainer(rowDatas)) { //验证成功后才查询
            var params = [];
            for (var i = 0; i < rowDatas.length; i++) {
                var param = {
                    productID: rowDatas[i].PRODUCTID,
                    mfgOrderName: rowDatas[i].MFGORDERNAME,
                    togetherflag: rowDatas[i].TOGETHERFLAG,
                    containerType: rowDatas[i].CONTAINERTYPE,
                    containerNo: rowDatas[i].CONTAINERNO,
                    //                    workflowRev: rowDatas[i].WORKFLOWREVISION,
                    workflowStepId: rowDatas[i].WORKFLOWSTEPID,
                    factoryId: factoryId
                };
                params.push(param);
            }
            var ajaxParam = { action: "getContainerDetails", params: JSON.stringify(params) };
            page.post(page.url, ajaxParam, true, function (data) {
                if (data.Result) {
                    $("#gdContainerInfo2").datagrid('loadData', { rows: data.DBTable });
                    if (data.DBTable.length === 1) {
                        $("#gdContainerInfo2").datagrid('selectRow', 0); //如果只有一个批次，选中
                    }
                }
                displayMessage(data.Message, data.Result);
            });
        }
    },
    //主批次查询
    search: function () {
        var containerName = $.trim($('#txtContainerName').textbox('getValue'));
        var scanContainerName = $.trim($("#txtScanContainer").textbox('getValue'));
        var orderNo = $.trim($('#txtOrderNo').textbox('getValue'));
        var productName = $.trim($('#txtProductName').textbox('getValue'));
        var stepNo = $.trim($('#txtWorkFlowStepNo').textbox('getValue'));
        var factoryId = $('#selFactory').combobox('getValue');
        var queryData = {
            action: "GetContainerList",
            pageSize: page.pageSize,
            pageNumber: page.pageNumber,
            containerName: containerName,
            scanContainerName: scanContainerName,
            orderNo: orderNo,
            productName: productName,
            stepNo: stepNo,
            factoryId: factoryId
        };
        page.post(page.url, queryData, true, function (res) {
            if (res.Result) {
                //批次列表
                page.pagetotal = res.RowCount;
                $("#gdContainerTable").datagrid('loadData', { total: res.RowCount, rows: res.DBTable });
            }
        });
    },
    //详细批次列表事件
    gd2DatagridEvent: function () {
        var typearr = page.pageType.split(',');
        for (var i = 0; i < typearr.length; i++) {
            switch (typearr[i]) {
                case "1": //不加工
                    stepNoShip.bindDatas();
                    break;
                case "2": //工序备注
                    stepAttr.bindDatas();
                    break;
                case "3": //批次属性
                    containerAttr.bindDatas();
                    break;
                case "4": //配置表
                    accessoriesAttr.bindDatas();
                    break;
                case "5": //报工备注
                    stepWorkReport.bindDatas();
                    break;
                default:
                    break;
            }
        }
    },
    //清空页面填写区域的数据(查询区域，批次主列表，详细批次列表，不加工，工序属性，批次属性，配置表，报工备注)
    clearPageData: function (query, containerList, containerDetaileList, noShip, stepAttr, containerAttr, Accessories, workReport) {
        clearMessage();
        if (query) {
            $('#txtOrderNo').textbox('setValue', "");
            $('#txtProductName').textbox('setValue', "");
            $('#txtContainerName').textbox('setValue', "");
            $('#txtWorkFlowStepNo').textbox('setValue', "");
        }
        if (containerList) {
            clearEasyuiDataGrid("gdContainerTable", 1); //清空批次列表
        }
        if (containerDetaileList) {
            clearEasyuiDataGrid("gdContainerInfo2", 1); //清空批次详细列表
        }
        if (noShip) {
            clearEasyuiDataGrid("gdStepNoShip", 1);
            clearEasyuiDataGrid("gdStepNoShip2", 1);
        }
        if (stepAttr) {
            $('#txtStepAttrRemark').textbox('setValue', "");
            clearEasyuiDataGrid("gdStepTable", 1);
        }
        if (containerAttr) {
            $('#txtEquipmentCheck').textbox('setValue', "");
            $('#txtCustomDep').textbox('setValue', "");
        }
        if (Accessories) {
            $('#txtAccessoriesRemark').textbox('setValue', "");
            clearEasyuiDataGrid("gdAccessoriesTable", 1);
        }
        if (workReport) {
            $('#txtWorkReportNotes').textbox('setValue', "");
            clearEasyuiDataGrid("gdStepWorkReport", 1);
        }
    },
    //验证选择的批次是否有效
    isVaildContainer: function (rows) {
        if (rows.length === 0) {
            return false;
        }
        if (page.pageType === "3") {//如果是批次属性，不考虑验证
            return true;
        }
        var vaild = true;
        for (var i = 0; i < rows.length; i++) {
            if (i + 1 === rows.length) {
                break;
            }
            if (rows[i].MFGORDERNAME !== rows[i + 1].MFGORDERNAME) {
                displayMessage("选择的批次订单不一致", false);
                vaild = false;
                break;
            }
            if (rows[i].PRODUCTID !== rows[i + 1].PRODUCTID) {
                displayMessage("选择的批次件号不一致", false);
                vaild = false;
                break;
            }
            if (rows[i].WORKFLOWID !== rows[i + 1].WORKFLOWID) {
                displayMessage("选择的批次工艺不一致", false);
                vaild = false;
                break;
            }
        }
        return vaild;
    },
    //url参数验证
    pageVaild: function (arr) {
        if (arr.length === 0) {
            displayMessage("未指定功能类型", false);
            return false;
        }
        var isvaild = true;
        for (var i = 0; i < arr.length; i++) {
            if (arr[i] !== "1" && arr[i] !== "2" && arr[i] !== "3" && arr[i] !== "4" && arr[i] !== "5") {
                displayMessage("参数出错", false);
                isvaild = false;
                break;
            }
        }
        return isvaild;
    },
    //post数据（路径，参数，是否异步，成功返回方法）
    post: function (url, param, async, callback) {
        $.ajax({
            type: "Post",
            url: url,
            data: param,
            cashe: false,
            async: async,
            dataType: "json",
            beforeSend: function () {
                $.messager.progress({ text: '', msg: '处理中...' });
            },
            success: function (data) {
                $.messager.progress('close');
                callback(data);
            },
            error: function (XMLHttpRequest, textStatus, errorThrown) {
                $.messager.progress('close');
                displayMessage(XMLHttpRequest.responseText, false);
                $("body").find(".easyui-linkbutton").each(function () {
                    $(this).linkbutton('enable');
                });
            }
        });
    }
};
//工序不加工对象
var stepNoShip = {
    init: function () {
        stepNoShip.bind();
    },
    //绑定事件
    bind: function () {
        //工序不加工-工序列表
        $("#gdStepNoShip").datagrid({
            columns: [[
               { field: 'ck', title: '选择', checkbox: true, width: 50 },
		         { field: 'SPECNO', title: '工序号', width: 120 },
                 { field: 'OPDESC', title: '工序名称', width: 250 },
                 { field: 'WORKFLOWSTEPID', hidden: true }
                ]]
        });
        //工序不加工-不加工工序列表
        $("#gdStepNoShip2").datagrid({
            columns: [[
               { field: 'ck', title: '选择', checkbox: true, width: 50 },
                 { field: 'CONTAINERNAME', title: '流水卡号', width: 150 },
		         { field: 'SPECNO', title: '工序号', width: 120 },
                 { field: 'OPDESC', title: '工序名称', width: 180 },
                 { field: 'FULLNAME', title: '创建人', width: 120 },
                 { field: 'CREATEDATE', title: '创建时间', width: 120,
                     formatter: function (value) {
                         if (value !== null) {
                             return dayjs(value).format("YYYY-MM-DD");
                         }
                     }
                 },
                 { field: 'WORKFLOWSTEPID', hidden: true },
                 { field: 'CONTAINERSTEPNOSHIPID', hidden: true }
                ]]
        });
        //添加工序不加工
        $('#btnStepNoShipSave').on("click", function () {
            stepNoShip.addDatas();
        });
        //移除工序不加工
        $('#btnStepNoShipClear').on("click", function () {
            stepNoShip.removeDatas();
        });
    },
    //绑定数据
    bindDatas: function () {
        stepNoShip.clearPage();
        var postForm = {};
        var dtContainers = [];
        //批次列表
        var crows = $("#gdContainerInfo2").datagrid('getChecked');
        if (crows.length == 0) {
            return;
        }
        var containerIds = "";
        var workFlowID = crows[0].WORKFLOWID;
        for (var i = 0; i < crows.length; i++) {
            if (workFlowID !== crows[i].WORKFLOWID) {
                workFlowID = "";
                break;
            }
            containerIds += crows[i].CONTAINERID + ",";
        }
        if (workFlowID === "") {
            displayMessage("所选批次工艺不一致", false);
            return;
        }
        containerIds = containerIds.substr(0, containerIds.length - 1);
        postForm = { action: "GetStepNoShipList", workFlowID: crows[0].WORKFLOWID, containerIds: containerIds };
        page.post(page.url, postForm, true, function (result) {
            if (result.Result === false) {
                displayMessage(result.Message, result.Result);
                return;
            }
            //绑定数据
            $("#gdStepNoShip").datagrid('loadData', { rows: result.DBDataSet.dtStep });
            $("#gdStepNoShip2").datagrid('loadData', { rows: result.DBDataSet.dtNoShip });
        });
    },
    //添加工序不加工数据
    addDatas: function () {
        clearMessage(); //清空提示消息
        var postFormData = {
            action: "AddStepNoShipDatas"
        };
        $('#btnStepNoShipSave').linkbutton('disable'); //禁用按钮
        $('#btnStepNoShipClear').linkbutton('disable'); //禁用按钮
        var stepNoShipRows = $("#gdStepNoShip").datagrid('getChecked');
        if (stepNoShipRows.length == 0) {
            displayMessage2("请选择工序", false, ["btnStepNoShipSave", "btnStepNoShipClear"]);
            return;
        }
        //批次列表
        var crows = $("#gdContainerInfo2").datagrid('getChecked');
        if (crows.length == 0) {
            displayMessage2("请选择批次信息", false, ["btnStepNoShipSave", "btnStepNoShipClear"]);
            return;
        }
        var dtContainers = [];
        for (var i = 0; i < crows.length; i++) {
            var param = {
                ContainerId: crows[i].CONTAINERID,
                ContainerName: crows[i].CONTAINERNAME
            };
            dtContainers.push(param);
        }
        postFormData.stepDt = JSON.stringify(stepNoShipRows); //已选工序
        postFormData.containerDt = JSON.stringify(dtContainers); //已选批次
        page.post(page.url, postFormData, true, function (result) {
            if (result.Result) {
                stepNoShip.bindDatas(crows);
            }
            displayMessage2(result.Message, result.Result, ["btnStepNoShipSave", "btnStepNoShipClear"]);
        });
    },
    //移除工序不加工数据
    removeDatas: function () {
        clearMessage(); //清空提示消息
        var postFormData = {
            action: "RemoveStepNoShipDatas"
        };
        $('#btnStepNoShipSave').linkbutton('disable'); //禁用按钮
        $('#btnStepNoShipClear').linkbutton('disable'); //禁用按钮
        var stepNoShipRows = $("#gdStepNoShip2").datagrid('getChecked');
        if (stepNoShipRows.length == 0) {
            displayMessage2("请选择不加工工序", false, ["btnStepNoShipSave", "btnStepNoShipClear"]);
            return;
        }
        postFormData.stepNoShipDt = JSON.stringify(stepNoShipRows); //已选不加工工序
        page.post(page.url, postFormData, true, function (result) {
            if (result.Result) {
                for (var i = 0; i < stepNoShipRows.length; i++) {
                    var index = $("#gdStepNoShip2").datagrid('getRowIndex', stepNoShipRows[i]);
                    $("#gdStepNoShip2").datagrid('deleteRow', index);
                }
            }
            displayMessage2(result.Message, result.Result, ["btnStepNoShipSave", "btnStepNoShipClear"]);
        });
    },
    clearPage: function () {
        clearMessage();
        clearEasyuiDataGrid("gdStepNoShip", 1);
        clearEasyuiDataGrid("gdStepNoShip2", 1);
    }
};
//批次属性对象
var containerAttr = {
    init: function () {
        containerAttr.bind();
    },
    //绑定事件
    bind: function () {
        //保存批次属性
        $('#btnContainerAttrSave').on("click", function () {
            containerAttr.postContainerAttrData(false);
        });
        //清除批次属性
        $('#btnContainerAttrClear').on("click", function () {
            containerAttr.postContainerAttrData(true);
        });
    },
    //绑定数据
    bindDatas: function () {
        containerAttr.clearPage();
        //批次列表
        var crows = $("#gdContainerInfo2").datagrid('getChecked');
        if (crows.length == 0) {
            return;
        }
        var containers = "";
        for (var i = 0; i < crows.length; i++) {
            containers += crows[i].CONTAINERID + ",";
        }
        containers = containers.substr(0, containers.length - 1);
        var postForm = { action: "GetContainerAttr", workFlowID: crows[0].WORKFLOWID, containerId: containers, bomId: crows[0].BOMID };
        page.post(page.url, postForm, true, function (result) {
            if (result.Result == false) {
                displayMessage(result.Message, result.Result);
                return;
            }
            //批次属性
            $('#txtEquipmentCheck').textbox('setValue', result.DBTable[0].EquipmentCheck);
            $('#txtCustomDep').textbox('setValue', result.DBTable[0].CustomDep);
        });
    },
    //提交批次属性数据（是否清除）
    postContainerAttrData: function (isClear) {
        var postFormData = {
            action: "SaveContainerAttrDatas"
        };
        $('#btnContainerAttrSave').linkbutton('disable'); //禁用按钮
        postFormData.equipmentCheck = "";
        postFormData.customDep = "";
        if (isClear == false) { //不是清除，则验证填写数据
            postFormData.equipmentCheck = $.trim($('#txtEquipmentCheck').textbox('getValue')); //器材检验
            postFormData.customDep = $.trim($('#txtCustomDep').textbox('getValue')); //顾客代表
            if (isNullOrWhiteSpaceStr(postFormData.equipmentCheck) && isNullOrWhiteSpaceStr(postFormData.customDep)) {
                displayMessage2("请填写至少一项数据", false, ["btnContainerAttrSave"]);
                return;
            }
        }
        //批次列表
        var crows = $("#gdContainerInfo2").datagrid('getChecked');
        if (crows.length == 0) {
            displayMessage2("请选择批次信息", false, ["btnContainerAttrSave"]);
            return;
        }
        var dtContainers = [];
        for (var i = 0; i < crows.length; i++) {
            var param = {
                ContainerId: crows[i].CONTAINERID
            };
            dtContainers.push(param);
        }
        postFormData.containerDt = JSON.stringify(dtContainers); //已选批次
        page.post(page.url, postFormData, true, function (result) {
            if (result.Result) {
                page.clearPageData(false, false, false, false, false, true, false, false);
                $("#gdContainerInfo2").datagrid('uncheckAll');
            }
            displayMessage2(result.Message, result.Result, ["btnContainerAttrSave"]);
        });
    },
    clearPage: function () {
        clearMessage();
        $('#txtEquipmentCheck').textbox('setValue', "");
        $('#txtCustomDep').textbox('setValue', "");
    }
};
//批次工序属性对象
var stepAttr = {
    init: function () { stepAttr.bind(); stepAttr.initData(); },
    bind: function () {
        //工序列表
        $("#gdStepTable").datagrid({
            columns: [[
               { field: 'ck', title: '选择', checkbox: true, width: 30 },
		         { field: 'SPECNO', title: '工序号', width: 100 },
                 { field: 'OPDESC', title: '工序名称', width: 260 },
                 { field: 'NOTES', title: '备注', width: 180 },
                 { field: 'WORKFLOWSTEPID', hidden: true },
                 { field: 'CONTAINERSTEPATTRIBUTEID', hidden: true },
                ]],
            onSelect: function (rowIndex, rowData) {
                $("#txtStepAttrRemark").textbox('setValue', rowData.NOTES);
            },
            onUnselect: function (rowIndex, rowData) {
                $("#gdStepTable").datagrid("uncheckAll");
                $("#txtStepAttrRemark").textbox('setValue', "");
            }
        });
        //保存工序属性
        $('#btnStepAttrSave').on("click", function () {
            stepAttr.postStepAttrData(false);
        });
        //清除工序属性
        $('#btnStepAttrClear').on("click", function () {
            stepAttr.postStepAttrData(true);
        });
        //显示所有工序
        $("#isShowAllStep").on("click", function () {
            stepAttr.bindDatas();
        });
        //        //添加常用术语
        //        $("#btnTermAdd").on("click", function () {
        //            stepAttr.postCommonTerm(false);
        //        });
        //        //添加常用术语
        //        $("#btnTermDele").on("click", function () {
        //            stepAttr.postCommonTerm(true);
        //        });
    },
    //初始化工序备注默认数据
    initData: function () {
        page.post(page.url, { action: "GetUserRoleList" }, true, function (res) {
            if (res.Result && res.DBTable.length > 0) {
                //绑定工序备注常用语角色下拉列表
                $("#selQRole").combobox({ //查询下拉
                    valueField: 'ROLEID',
                    textField: 'ROLENAME',
                    panelHeight: 200,
                    data: res.DBTable, //下拉数据
                    value: res.DBTable[0].ROLEID,
                    onSelect: function (record) {
                        stepAttr.bindCommonTerm(record.ROLENAME);
                    }
                });
                //                    $("#selMRole").combobox({ //维护下拉
                //                        valueField: 'ROLEID',
                //                        textField: 'ROLENAME',
                //                        panelHeight: 200,
                //                        data: res.DBTable //下拉数据
                //                    });

            }
        });
    },
    //绑定数据
    bindDatas: function () {
        stepAttr.clearPage();
        //批次列表
        var crows = $("#gdContainerInfo2").datagrid('getChecked');
        if (crows.length == 0) {
            return;
        }
        var containers = "";
        for (var i = 0; i < crows.length; i++) {
            containers += crows[i].CONTAINERID + ",";
        }
        containers = containers.substr(0, containers.length - 1);
        var mrow = $("#gdContainerTable").datagrid('getChecked');
        var sequence = mrow[0].WORKFLOWSTEPSEQUENCE;
        if ($("#isShowAllStep").prop("checked")) {
            sequence = "";
        }
        var postForm = { action: "GetStepAttrList", workFlowID: crows[0].WORKFLOWID, containerId: containers, sequence: sequence };
        page.post(page.url, postForm, true, function (result) {
            if (result.Result == false) {
                displayMessage(result.Message, result.Result);
                return;
            }
            //绑定数据
            $("#gdStepTable").datagrid('loadData', { rows: result.DBTable });
        });
    },
    //提交工序属性数据（是否清除）
    postStepAttrData: function (isClear) {
        var postFormData = {
            action: "SaveStepAttrDatas"
        };
        $('#btnStepAttrSave').linkbutton('disable'); //禁用按钮
        $('#btnStepAttrClear').linkbutton('disable'); //禁用按钮
        var stepRows = $("#gdStepTable").datagrid('getChecked');
        if (stepRows.length == 0) {
            displayMessage2("请选择工序", false, ["btnStepAttrSave", "btnStepAttrClear"]);
            return;
        }
        if (isClear == false) { //不是清除，则验证填写数据
            postFormData.notes = $.trim($('#txtStepAttrRemark').textbox('getValue'));
            if (isNullOrWhiteSpaceStr(postFormData.notes)) {
                displayMessage2("请填写备注", false, ["btnStepAttrSave", "btnStepAttrClear"]);
                return false;
            }
        }
        //批次列表
        var crows = $("#gdContainerInfo2").datagrid('getChecked');
        if (crows.length == 0) {
            displayMessage2("请选择批次信息", false, ["btnStepAttrSave", "btnStepAttrClear"]);
            return;
        }
        var dtContainers = [];
        for (var i = 0; i < crows.length; i++) {
            var param = {
                ContainerId: crows[i].CONTAINERID
            };
            dtContainers.push(param);
        }
        postFormData.containerDt = JSON.stringify(dtContainers); //已选批次
        postFormData.stepDatas = JSON.stringify(stepRows); //已选工序
        postFormData.isClear = isClear;
        page.post(page.url, postFormData, true, function (result) {
            if (result.Result) {
                stepAttr.bindDatas();
            }
            displayMessage2(result.Message, result.Result, ["btnStepAttrSave", "btnStepAttrClear"]);
        });
    },
    //常用术语添加至备注
    tagNotes: function (text) {
        var notes = "";
        notes = $.trim($('#txtStepAttrRemark').textbox('getValue'));
        if (isNullOrWhiteSpaceStr(notes)) {
            notes = text;
        }
        else {
            notes += "," + text;
        }
        //        else {
        //            notes = notes.replace(text, "").replace(",,", ",");
        //            if (type === 1) { //追加
        //                notes += "," + text;
        //            }
        //        }
        //        var st = notes.split(",");
        //        notes = "";
        //        for (var i = 0; i < st.length; i++) {
        //            if (st[i] !== "") {
        //                notes += st[i] + ",";
        //            }
        //        }
        //        notes = notes.substr(0, notes.length - 1);
        $('#txtStepAttrRemark').textbox('setValue', notes);
    },
    //绑定标签列表(通过角色名)
    bindCommonTerm: function (roleName) {
        clearMessage();
        $('#txtCommonTerm').textbox('setText', "");
        $("#selMRole").combobox("setValue", "");
        var postForm = { action: "GetCommonTerm", roleName: roleName };
        page.post(page.url, postForm, true, function (result) {
            if (result.Result) {
                var data = result.DBTable;
                var htmls = "";
                for (var i = 0; i < data.length; i++) {
                    htmls += "<a class='easyui-linkbutton a-term' data-fid='" + data[i].FACTORYCOMMONTERMID + "' style='border-color: #dce1e6; margin:5px;'>" + data[i].TERMVALUE + "</a>"
                }
                $("#commonTerm").html(htmls);
                $.parser.parse("#commonTerm"); //局部刷新样式
                $("#commonTerm").find(".easyui-linkbutton").each(function () {
                    $(this).on("click", function () {
                        var text = $(this).text();
                        var stepRows = $("#gdStepTable").datagrid('getChecked');
                        if (stepRows.length > 0) { //如果选择了工序
                            stepAttr.tagNotes(text); //追加备注
                        }

                    });
                })
            }
        });
    },
    //保存常用术语（是否删除）
    postCommonTerm: function (isDele) {
        var postFormData = {
            action: "SaveCommonTerm",
            roleName: "",
            termValue: "",
            factorycommontermId: "",
            isDelete: isDele
        };
        $('#btnTermAdd').linkbutton('disable'); //禁用按钮
        $('#btnTermDele').linkbutton('disable'); //禁用按钮
        if (isDele) {
            $("#commonTerm").find(".easyui-linkbutton").each(function () {
                var color = $(this).css("border-color");
                var text = $(this).attr("");
                if (color === "red") {
                    var fid = $(this).attr("data-fid");
                    postFormData.factorycommontermId += "'" + fid + "',";
                }
            });
            if (isNullOrWhiteSpaceStr(postFormData.factorycommontermId)) {
                displayMessage2("请选择术语", false, ["btnTermAdd", "btnTermDele"]);
                return false;
            }
        } else {
            postFormData.roleName = $('#selMRole').combobox('getText');
            if (isNullOrWhiteSpaceStr(postFormData.roleName)) {
                displayMessage2("请选择角色", false, ["btnTermAdd", "btnTermDele"]);
                return false;
            }
            postFormData.termValue = $('#txtCommonTerm').textbox('getText');
            if (isNullOrWhiteSpaceStr(postFormData.termValue)) {
                displayMessage2("请填写术语", false, ["btnTermAdd", "btnTermDele"]);
                return false;
            }
        }
        page.post(page.url, postFormData, true, function (result) {
            if (result.Result) {
                stepAttr.bindCommonTerm($('#selQRole').combobox('getText'));
            }
            displayMessage2(result.Message, result.Result, ["btnTermAdd", "btnTermDele"]);
        });
    },
    clearPage: function () {
        clearMessage();
        $('#txtStepAttrRemark').textbox('setValue', "");
        clearEasyuiDataGrid("gdStepTable", 1);
    }
};
//配置表属性对象
var accessoriesAttr = {
    init: function () { accessoriesAttr.bind(); },
    bind: function () {
        //配套件列表
        $("#gdAccessoriesTable").datagrid({
            columns: [[
               { field: 'ck', title: '选择', checkbox: true, width: 30 },
		         { field: 'PRODUCTINFO', title: '件号信息', width: 180 },
                 { field: 'SPECNAME', title: '工序', width: 200 },
                 { field: 'NOTES', title: '备注', width: 260 },
                 { field: 'ISBOMPRODUCT', hidden: true },
                 { field: 'PRODUCTID', hidden: true }
                ]],
            onSelect: function (rowIndex, rowData) {
                $("#txtAccessoriesRemark").textbox('setValue', rowData.NOTES);
            },
            onUnselect: function (rowIndex, rowData) {
                $("#gdAccessoriesTable").datagrid("uncheckAll");
                $("#txtAccessoriesRemark").textbox('setValue', "");
            }
        });
        //保存配置表属性
        $('#btnAccessoriesSave').on("click", function () {
            accessoriesAttr.postAccessoriesAttrData(false);
        });
        //清除配置表属性
        $('#btnAccessoriesClear').on("click", function () {
            accessoriesAttr.postAccessoriesAttrData(true);
        });
    },
    bindDatas: function () {
        accessoriesAttr.clearPage();
        //批次列表
        var crows = $("#gdContainerInfo2").datagrid('getChecked');
        if (crows.length == 0) {
            return;
        }
        var isError = false;
        var containers = "";
        var bomID = crows[0].BOMID;
        for (var i = 0; i < crows.length; i++) {
            containers += crows[i].CONTAINERID + ",";
            if (bomID !== crows[i].BOMID) {
                isError = true;
                break;
            }
        }
        if (isError) {
            displayMessage("所选批次Bom数据不一致", false);
            return;
        }
        containers = containers.substr(0, containers.length - 1);
        var postForm = { action: "GetBomProductAttr", containerId: containers, bomId: bomID };
        page.post(page.url, postForm, true, function (result) {
            if (result.Result == false) {
                displayMessage(result.Message, result.Result);
                return;
            }
            //绑定数据
            $("#gdAccessoriesTable").datagrid('loadData', { rows: result.DBTable });
        });
    },
    //提交配置表属性数据（是否清除）
    postAccessoriesAttrData: function (isClear) {
        var postFormData = {
            action: "SaveAccessoriesAttrDatas"
        };
        $('#btnAccessoriesSave').linkbutton('disable'); //禁用按钮
        var accessoriesRows = $("#gdAccessoriesTable").datagrid('getChecked');
        if (accessoriesRows.length == 0) {
            displayMessage2("请选择配置表数据", false, ["btnAccessoriesSave", "btnAccessoriesClear"]);
            return;
        }
        postFormData.bomProducts = JSON.stringify(accessoriesRows);
        if (isClear == false) { //如果不是清除，则验证配置表属性
            postFormData.notes = $.trim($('#txtAccessoriesRemark').textbox('getValue'));
            if (isNullOrWhiteSpaceStr(postFormData.notes)) {
                displayMessage2("请填写配置表备注", false, ["btnAccessoriesSave", "btnAccessoriesClear"]);
                return false;
            }
        }
        //批次列表
        var crows = $("#gdContainerInfo2").datagrid('getChecked');
        if (crows.length == 0) {
            displayMessage2("请选择批次信息", false, ["btnAccessoriesSave", "btnAccessoriesClear"]);
            return;
        }
        var dtContainers = [];
        for (var i = 0; i < crows.length; i++) {
            var param = {
                ContainerId: crows[i].CONTAINERID
            };
            dtContainers.push(param);
        }
        postFormData.containerDt = JSON.stringify(dtContainers); //已选批次
        postFormData.isClear = isClear;
        page.post(page.url, postFormData, true, function (result) {
            if (result.Result) {
                accessoriesAttr.bindDatas();
            }
            displayMessage2(result.Message, result.Result, ["btnAccessoriesSave", "btnAccessoriesClear"]);
        });
    },
    clearPage: function () {
        clearMessage();
        $('#txtAccessoriesRemark').textbox('setValue', "");
        clearEasyuiDataGrid("gdAccessoriesTable", 1);
    }
};
//工序备注维护
var stepWorkReport = {
    //初始化
    init: function () {
        stepWorkReport.bind();
    },
    //绑定事件
    bind: function () {
        //工序报工备注列表
        $("#gdStepWorkReport").datagrid({
            columns: [[
                 { field: 'ck', title: '选择', checkbox: true, width: 30 },
		         { field: 'SPECNO', title: '工序号', width: 60 },
                 { field: 'OPDESC', title: '工序名称', width: 100 },
                 { field: 'CHECKTYPENAME', title: '报工类型', width: 80 },
                 { field: 'WORKERNAME', title: '报工人', width: 60 },
                 { field: 'CREATEDATE', title: '报工时间', width: 80,
                     formatter: function (value, row, index) {
                         if (!isNullOrWhiteSpaceStr(value)) {
                             return dayjs(value).format('YYYY-MM-DD');
                         }
                     }
                 },
                 { field: 'NOTES', title: '报工备注', width: 180 },
                 { field: 'WORKFLOWSTEPID', hidden: true },
                 { field: 'CONTAINERSTEPWORKREPORTID', hidden: true },
                ]],
            onSelect: function (rowIndex, rowData) {
                $("#txtWorkReportNotes").textbox('setValue', rowData.NOTES);
            },
            onUnselect: function (rowIndex, rowData) {
                $("#gdStepWorkReport").datagrid("uncheckAll");
                $("#txtWorkReportNotes").textbox('setValue', "");
            }
        });
        //保存报工备注
        $('#btnSaveWorkReportNotes').on("click", function () {
            stepWorkReport.postData();
        });
    },
    //加载列表数据
    bindDatas: function () {
        stepWorkReport.clearPage();
        //批次列表
        var crows = $("#gdContainerInfo2").datagrid('getChecked');
        if (crows.length === 0) {
            return;
        }
        var containers = "";
        for (var i = 0; i < crows.length; i++) {
            containers += crows[i].CONTAINERID + ",";
        }
        containers = containers.substr(0, containers.length - 1);
        var postForm = { action: "GetStepWorkReportList", containerIds: containers };
        page.post(page.url, postForm, true, function (result) {
            if (result.Result === false) {
                displayMessage(result.Message, result.Result);
                return;
            }
            //绑定数据
            $("#gdStepWorkReport").datagrid('loadData', { rows: result.DBTable });
        });
    },
    //提交数据
    postData: function () {
        $('#btnSaveWorkReportNotes').linkbutton('disable'); //禁用按钮
        var rows = $("#gdStepWorkReport").datagrid('getChecked');
        if (rows.length === 0) {
            displayMessage2("请选择报工记录", false, ["btnSaveWorkReportNotes"]);
            return;
        }
        var workreportIDs = "";
        for (var i = 0; i < rows.length; i++) {
            workreportIDs += rows[i].CONTAINERSTEPWORKREPORTID + ",";
        }
        workreportIDs = workreportIDs.substr(0, workreportIDs.length - 1);
        var postForm = {
            action: "SaveStepWorkReportData",
            notes: $('#txtWorkReportNotes').textbox('getValue'),
            workreportIDs: workreportIDs
        };
        page.post(page.url, postForm, true, function (result) {
            if (result.Result) {
                stepWorkReport.bindDatas();
            }
            displayMessage2(result.Message, result.Result, ["btnSaveWorkReportNotes"]);
        });
    },
    //清除页面
    clearPage: function () {
        clearMessage();
        $('#txtWorkReportNotes').textbox('setValue', "");
        clearEasyuiDataGrid("gdStepWorkReport", 1);
    }
};
page.init();